package com.bloomingstars.sqm.util;

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.Logger;

public class StackTraceLogger {

    private Logger myLogger;
    
    public StackTraceLogger(Logger myLogger) {
        this.myLogger = myLogger;
    }
    
    /**
     * Logs the full stack trace of the exception to myLogger
     */
    public void logStackTrace(Throwable t) {
        myLogger.error("Full stack trace follows: ");  
        logStackTrace("", t);
    }
    
    public void logStackTrace(String requiredSubstring, Throwable t) {
        logStackTrace(Level.ERROR, requiredSubstring, t);
    }
    
    public void logStackTrace(Level priority, String requiredSubstring, Throwable t) {
        myLogger.log(priority, "Filtered stack trace follows: ");
        for (StackTraceElement element: t.getStackTrace()) {
            if (!requiredSubstring.equalsIgnoreCase("")) {
                if (element.toString().indexOf(requiredSubstring) != -1) {
                    myLogger.log(priority, element.toString());
                }
            } else {
                myLogger.log(priority, element.toString());
            }            
        }               
    }
}
